package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//创建一个生产者消费者模型的代码
//创建一个线程来模拟生产者
//创建一个模型来模拟消费者
//创建一个阻塞队列
public class Demo10 {
    public static void main(String[] args) {
        BlockingQueue <Long> queue=new ArrayBlockingQueue<>(1000);
        //生产者
        Thread t1=new Thread(()->{
            long n=0;
            while (true){
                try {
                    queue.put(n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("生产了"+n);
                n++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        //消费者
        Thread t2=new Thread(()->{
            while (true){
                try {
                   Long n = queue.take();
                    System.out.println("消费了"+n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
